package com.gsi.webstore.platform.storefront.internal.order;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.gsi.webstore.platform.adapter.taxes_duties_fees.capi.calculation.ComputedTaxInfoItem;
import com.gsi.webstore.platform.storefront.capi.order.GSIExtraChargeInformationBO;
import com.gsi.webstore.platform.storefront.capi.order.GSITaxInformationBO;
import com.intershop.beehive.core.capi.component.ComponentMgr;
import com.intershop.beehive.core.capi.localization.LocaleInformation;
import com.intershop.beehive.core.capi.naming.NamingMgr;
import com.intershop.beehive.foundation.quantity.Money;
import com.intershop.component.foundation.capi.spreadsheet.ComputedMoneyItem;
import com.intershop.component.shipping.capi.shippingchargecalculator.ComputedShippingExtraChargeItem;
import com.intershop.component.shipping.capi.shippingrule.ShippingRule;
import com.intershop.component.shipping.capi.shippingrule.ShippingRuleRepository;

public class GSIExtraChargeInformationBOImpl implements GSIExtraChargeInformationBO
{
    private final ComputedShippingExtraChargeItem _cseci;
    private final ComputedMoneyItem _cmi;
    private final Collection<ComputedTaxInfoItem> _ctii_appliedTaxes;
    private Collection<GSITaxInformationBO> _appliedTaxes;
    private ShippingRuleRepository shippingRuleRepository;
    private ComponentMgr componentMgr;

    public GSIExtraChargeInformationBOImpl(final ComputedShippingExtraChargeItem cseci, final ComputedMoneyItem cmi,
                    final Collection<ComputedTaxInfoItem> ctii_appliedTaxes)
    {
        _cseci = cseci;
        _cmi = cmi;
        _ctii_appliedTaxes = ctii_appliedTaxes;

        componentMgr = NamingMgr.getManager(ComponentMgr.class);
        shippingRuleRepository = componentMgr.getComponent("shippingRuleRepository");
    }

    @Override
    public Money getAmount()
    {
        return _cmi.getMoney();
    }

    @Override
    public String getID()
    {
        // shipping rule's ID
        ShippingRule rule = _cseci.getShippingRule();
        if (rule != null)
        {
            rule = shippingRuleRepository.get(_cseci.getShippingRuleID());

            if (rule != null)
            {
                return rule.getId();
            }

        }
        return null;
    }

    @Override
    public String getDisplayName(final LocaleInformation locale)
    {
        // shipping rule's display name
        ShippingRule rule = _cseci.getShippingRule();
        if (rule != null)
        {
            rule = shippingRuleRepository.get(_cseci.getShippingRuleID());

            if (rule != null)
            {
                return rule.getDisplayName(locale);
            }
        }
        return null;
    }

    @Override
    public String getDescription(final LocaleInformation locale)
    {
        // shipping rule's description
        ShippingRule rule = _cseci.getShippingRule();
        if (rule != null)
        {
            rule = shippingRuleRepository.get(_cseci.getShippingRuleID());

            if (rule != null)
            {
                return rule.getDescription(locale);
            }
        }
        return null;
    }

    @Override
    public Collection<GSITaxInformationBO> getExtraChargeBasedTaxes()
    {
        if (_appliedTaxes != null)
        {
            return _appliedTaxes;
        }

        if (_ctii_appliedTaxes.isEmpty())
        {
            return Collections.emptyList();
        }

        final List<GSITaxInformationBO> result = new ArrayList<GSITaxInformationBO>(_ctii_appliedTaxes.size());
        for(final ComputedTaxInfoItem ctii : _ctii_appliedTaxes)
        {
            result.add(new GSITaxInformationBOImpl(ctii));
        }
        return result;

    }
}